﻿using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Entity.TD;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.TD
{
    public partial class TD_MoveBusiness : BaseBusiness<TD_Move>, ITD_MoveBusiness, ITransientDependency
    {
        

        #region 外部接口

        public async Task<PageResult<TD_Move>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<TD_Move>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<TD_Move, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }




        [DataDeleteLog(UserLogType.移库管理, "Code", "移库单")]
        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

    

        public async Task<TD_MoveInfo> GetListSelection(string id)
        {
            Expression<Func<TD_Move, Base_Department, Base_User, TD_MoveInfo>> select = (a, b,c) => new TD_MoveInfo
            {
                CreatorIdName=c.RealName,
                StorIdName=b.Name,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.StorId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    join c in Db.GetIQueryable<Base_User>() on a.CreatorId equals c.Id into ac
                    from c in ac.DefaultIfEmpty()
                    select @select.Invoke(a, b,c);
           TD_MoveInfo listdata = new TD_MoveInfo();
            var qdata = await q.Where(x => x.Id==id).FirstOrDefaultAsync();
      
                Expression<Func<TD_MoveDetail, Base_Department, PB_Location, PB_Location, PB_Material,PB_StorArea,PB_Rack, PB_StorArea, PB_Rack, TD_MoveDetailDto>> select2 = (a, b, c, d, e,f,g,h,u) => new TD_MoveDetailDto
                {
                    StorIdName = b.Name,
                    FromLocalIdName = f.Name+"/"+g.Name+"/"+c.Name,
                    ToLocalIdName = h.Name+"/"+u.Name+"/"+d.Name,
                    MaterialIdName=e.Name,

                };
                select2 = select2.BuildExtendSelectExpre();
                var q2 = from a in Db.GetIQueryable<TD_MoveDetail>().AsExpandable()
                         join b in Db.GetIQueryable<Base_Department>() on a.StorId equals b.Id into ab
                         from b in ab.DefaultIfEmpty()

                         join c in Db.GetIQueryable<PB_Location>() on a.FromLocalId equals c.Id into ac
                         from c in ac.DefaultIfEmpty()

                         join d in Db.GetIQueryable<PB_Location>() on a.ToLocalId equals d.Id into ad
                         from d in ad.DefaultIfEmpty()

                         join e in Db.GetIQueryable<PB_Material>() on a.MaterialId equals e.Id into ae
                         from e in ae.DefaultIfEmpty()
                         join f in Db.GetIQueryable<PB_StorArea>() on c.AreaId equals f.Id into cf
                         from f in cf.DefaultIfEmpty()
                         join g in Db.GetIQueryable<PB_Rack>() on c.RackId equals g.Id into cg
                         from g in cg.DefaultIfEmpty()
                         join h in Db.GetIQueryable<PB_StorArea>() on d.AreaId equals h.Id into dh
                         from h in dh.DefaultIfEmpty()
                         join u in Db.GetIQueryable<PB_Rack>() on d.RackId equals u.Id into du
                         from u in du.DefaultIfEmpty()
                         select select2.Invoke(a, b, c,d,e,f,g,h,u);
                var res = await q2.Where(x => x.MoveId == qdata.Id).ToListAsync();
                TD_MoveInfo pblist = new TD_MoveInfo();
                pblist.Id = qdata.Id;
                pblist.Code = qdata.Code;
                pblist.CreatorIdName = qdata.CreatorIdName;
                pblist.StorIdName = qdata.StorIdName;
                pblist.MoveTime = qdata.MoveTime;
                pblist.Type = qdata.Type;
                pblist.RefCode = qdata.RefCode;
                pblist.StorId = qdata.StorId;
                pblist.MoveNum = qdata.MoveNum;
                pblist.TotalAmt = qdata.TotalAmt;
                pblist.EquId = qdata.EquId;
                pblist.Status = qdata.Status;
                pblist.SubDate = res;
            pblist.CreateTime = qdata.CreateTime;
                if (res != null)
                {
                    return pblist;
                }
                else
                {
                    throw new BusException("暂无数据");
                }
          
         
        }

        #endregion

        #region 私有成员

        #endregion
    }
}